const Pencils = require('./Pencils');

cc.Class({
    extends: cc.Component,

    properties: {
        pencilsPrefab: cc.Prefab,
        pencilsLayer: cc.Node,
        initX: 0,
        spawnInterval: 0
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        D.pencilsManger = this;
        this.pencilsPool = new cc.NodePool();
        this.schedule(this.spawnPencils, this.spawnInterval);
    },

    spawnPencils() {
        let pencils = null;
        if (this.pencilsPool.size() > 0) {
            pencils = this.pencilsPool.get();
        } else {
            pencils = cc.instantiate(this.pencilsPrefab);
        }
        this.pencilsLayer.addChild(pencils);
        // pencils.active = true;
        pencils.x = this.initX;
    },

    despawnPencils(pencils) {
        this.pencilsPool.put(pencils);
    },

    reset() {
        this.schedule(this.spawnPencils);
    }

});
